package com.miao.hong.fang.user.service;

import java.sql.Timestamp;
import java.util.List;

import com.miao.hong.fang.IConstants;
import com.miao.hong.fang.course.dao.CourseDAO;
import com.miao.hong.fang.user.UserAccessInfoTO;
import com.miao.hong.fang.user.UserSubscribeCourseTO;
import com.miao.hong.fang.user.UserTO;
import com.miao.hong.fang.user.dao.UserDAO;
import com.miao.hong.fang.util.SecurityUtil;
import com.miao.hong.fang.util.Utils;

public class UserServiceImpl implements UserService{

	private UserDAO userDAO;
	private CourseDAO courseDAO;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setCourseDAO(CourseDAO courseDAO) {
		this.courseDAO = courseDAO;
	}

	public boolean checkUserId(String userId) {
		return userDAO.checkUserId(userId);
	}
	
	public void registerUser(UserTO user) throws Exception {
		
		user.setCreatedBy("SYSTEM");
		user.setCreatedDt(new Timestamp(System.currentTimeMillis()));
		user.setUpdatedBy("SYSTEM");
		user.setUpdatedDt(new Timestamp(System.currentTimeMillis()));
		
		StringBuffer sb = new StringBuffer();
		sb.append(user.getPassword()).append("-").append(user.getLoginName()).append("-").
						append(Utils.timestampToString(user.getCreatedDt(), IConstants.DATE_TIME_FORMAT));
		user.setPassword(SecurityUtil.encryptPwd(sb.toString()));
		userDAO.registerUser(user);
	}

	public List getUserSubscribed(String userId) {
		List<UserSubscribeCourseTO> list = userDAO.getUserSubscribed(userId);
		
		for(UserSubscribeCourseTO to: list) {
			String courseId = to.getCourseId();
			to.setCourseTitle(courseDAO.getCourseDetail(courseId).getCourseTitle());
		}

		return list;
	}

	public UserTO getUserByLoginName(String loginName) {
		return userDAO.getUserByLoginName(loginName);
	}
	
	public UserAccessInfoTO getAccessInfo(String sessionId) {
		return userDAO.getAccessInfo(sessionId);
	}
	
	public void updateAccessInfo(UserAccessInfoTO to) {
		to.setCreatedDt(new Timestamp(System.currentTimeMillis()));
		userDAO.updateAccessInfo(to);
	}
	
	public void logAccessInfo(String sessionId, String loginName, String ipAddress) {
		UserAccessInfoTO to = new UserAccessInfoTO();
		to.setSessionId(sessionId);
		to.setLoginName(loginName);
		to.setIpAddress(ipAddress);
		to.setCreatedDt(new Timestamp(System.currentTimeMillis()));
		
		userDAO.logAccessInfo(to);
	}
	
	public void subscribeCourse(String userId, String courseId) {
		UserSubscribeCourseTO to = new UserSubscribeCourseTO();
		to.setUserId(userId);
		to.setCourseId(courseId);
		to.setCreatedBy(userId);
		to.setCreatedDt(new Timestamp(System.currentTimeMillis()));
		to.setUpdatedBy(userId);
		to.setUpdatedDt(new Timestamp(System.currentTimeMillis()));
		
		userDAO.subscribeCourse(to);
	}
	public boolean checkUserCourse(String userId, String courseId) {
		UserSubscribeCourseTO to = new UserSubscribeCourseTO();
		to.setUserId(userId);
		to.setCourseId(courseId);
		
		to = userDAO.checkUserCourse(to);
		return to==null ? false : true;
	}
	
	public void cancelSubscribeCourse(String userId, String[] courseIds) {
		
		for(String courseId:courseIds) {
			userDAO.cancelSubscribeCourse(userId, courseId);
		}
		
	}
}
